package uml.behavioral.mediator;

import java.util.HashSet;

/**
 * 中介者--抽象同事<br>
 * on 2023/1/2 22:27
 */
public abstract class User {
    protected String msg;//信息
    protected String name;//用户名
    protected HashSet<Mediator> groups;

    public User(String name) {
        groups = new HashSet<>();
        this.setName(name);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMsg() {
        return msg;
    }

    public String getName() {
        return name;
    }

    /**
     * 发送群聊消息
     *
     * @param msg 信息
     */
    public abstract void sendMessage(Mediator group, String msg);

    /**
     * 退出群聊
     *
     * @param group 群聊
     */
    public void quit(Mediator group) {
        this.groups.remove(group);
        group.quitGroup(this);
        System.out.println("系统消息：" + this.name + "退出群聊" + group.getName());
    }

    /**
     * 加入群聊
     *
     * @param group 群聊
     */
    public void joinGroup(Mediator group) {
        this.groups.add(group);
        group.join(this);
        System.out.println("系统消息：" + this.name + "加入群聊" + group.getName());
    }

}
